import { createSlice } from '@reduxjs/toolkit'

const userStore = createSlice({
    name: 'user',
    initialState: {
        user: JSON.parse(localStorage.getItem('user')) || {},
        token: localStorage.getItem('token') || '',
    },
    reducers: {
        setUser(state, action) {
            state.user = action.payload
            localStorage.setItem('user', JSON.stringify(action.payload))
        },
        setToken(state, action) {
            state.token = action.payload
            localStorage.setItem('token', action.payload)
        },
        removeToken(state) {
            state.token = ''
            localStorage.removeItem('token')
            localStorage.removeItem('user')
        }
    }
})
export default userStore.reducer

export const { setUser, setToken , removeToken} = userStore.actions
